/**
 * Create by 吃小笼包嘛？ on 2021
 * 工具类
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 */

/**
 * 防抖
 */
// function debounce(fn,delay){
// 	let debounceTimer = null;
// 	return function(){
// 		debounceTimer && clearTimeout(debounceTimer);
// 		debounceTimer = setTimeout(fn,delay);
// 	}
// }
/**
 * 防抖
 * @param {Object} fn 传入回调方法
 * @param {Object} delay 传入等待时间
 * @param {Object} immediate true=立即执行 false=先等待后执行
 */
function debounce(fn, delay=256, immediate=false) {
    let debounceTimer;
    return function () {
        if (debounceTimer) clearTimeout(debounceTimer);
        if (immediate) {
            // 如果已经执行过，不再执行
            var callNow = !debounceTimer;
            debounceTimer = setTimeout(() => {
                debounceTimer = null;
            }, delay)
            if (callNow) {
                fn.apply(this, arguments)
            }
        } else {
            debounceTimer = setTimeout(() => {
                fn.apply(this, arguments)
            }, delay);
        }
    }
}

/**
 * 获取当前是那个端
 */
function getUserAgent () {
	if (/MicroMessenger/.test(window.navigator.userAgent)) {
		return 'wechat';
	} else if (/AlipayClient/.test(window.navigator.userAgent)) {
		return 'alipay';
	} else {
		return 'web';
	}
	return null;
}

export default {
	debounce,
	getUserAgent,
}